﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Member;
using WuDada.Core.Member.Domain;
using WuDada.Core.Member.Service;

public partial class member_detail : System.Web.UI.Page
{
    private SessionHelper m_SessionHelper = new SessionHelper();
    private MemberFactory m_MemberFactory;
    private IMemberService m_MemberService;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_MemberFactory = new MemberFactory();
        m_MemberService = m_MemberFactory.GetMemberService();

        if (!Page.IsPostBack)
        {
            if (m_SessionHelper.Member != null)
            {
                MemberVO memberVO = m_SessionHelper.Member;
                UIHelper.FillUI(pnlUI, memberVO);
                bank.Value = memberVO.Bank;
                ckbRequirement.SelectedValue = memberVO.Requirement;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        MemberVO memberVO = m_MemberService.GetMemberByLoginId(m_SessionHelper.Member.LoginId);
        if (memberVO != null)
        {
            UIHelper.FillVO(pnlUI, memberVO);
            memberVO.Bank = bank.Value;
            memberVO.UpdatedDate = DateTime.Now;

            if (string.IsNullOrEmpty(ckbRequirement.SelectedValue))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("請勾選需求"), false);
            }
            else if (string.IsNullOrEmpty(memberVO.Area))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("請選擇縣市與鄉鎮市區"), false);
            }
            else if (!RegexHelper.CheckTwAddress(memberVO.Address))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("地址格式錯誤"), false);
            }

            if (string.IsNullOrEmpty(memberVO.City))
            {
                memberVO.Area = string.Empty;
            }
            if (string.IsNullOrEmpty(memberVO.CompanyCity))
            {
                memberVO.CompanyArea = string.Empty;
            }

            m_MemberService.UpdateMember(memberVO);
            m_SessionHelper.Member = memberVO;
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("修改資料成功!"), false);
        }
    }

    protected void btnSavePassword_Click(object sender, EventArgs e)
    {
        string oldPassword = txtOldPassword.Text.Trim();
        string newPassword = txtNewPassword.Text.Trim();
        string checkNewPassword = txtCheckNewPassword.Text.Trim();

        MemberVO memberVO = m_MemberService.GetMemberByLoginId(m_SessionHelper.Member.LoginId);
        if (memberVO != null)
        {
            if (!memberVO.Password.Equals(oldPassword))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("舊密碼輸入錯誤!"), false);
            }
            else if (!newPassword.Equals(checkNewPassword))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("確認密碼與密碼輸入不同，請重新輸入!"), false);
            }
            else
            {
                memberVO.Password = newPassword;
                memberVO.UpdatedDate = DateTime.Now;

                m_MemberService.UpdateMember(memberVO);
                m_SessionHelper.Member = memberVO;
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("修改密碼成功!"), false);
            }
        }
    }
}